package Work;
import java.util.Random;

import java.util.List;





import java.util.Scanner;

public class RandomAttendance {
    private Random random = new Random();

    private ClassManager classManager;

    private GroupManager groupManager;

    private StudentManager studentManager;

    private Scanner scanner = new Scanner(System.in);



    public RandomAttendance(ClassManager classManager, GroupManager groupManager, StudentManager studentManager) {

        this.classManager = classManager;

        this.groupManager = groupManager;

        this.studentManager = studentManager;

    }



    // 随机抽取班级

    public Class getRandomClass() {

        List<Class> classes = classManager.getClassList();

        return classes.get(random.nextInt(classes.size()));

    }



    // 随机抽取小组

    public Group getRandomGroup(String classId) {

        List<Group> groups = groupManager.getGroupList().stream()

                .filter(group -> group.getClassId().equals(classId))

                .toList();

        return groups.get(random.nextInt(groups.size()));

    }



    // 随机在小组中抽取学生

    public Student getRandomStudentInGroup(String groupId) {

        List<Student> students = studentManager.getStudentList().stream()

                .filter(student -> student.getGroupId().equals(groupId))

                .toList();

        return students.get(random.nextInt(students.size()));

    }



    // 给小组评分

    public void rateGroup(String groupId, int score) {

        System.out.println("小组 " + groupId + " 被评分: " + score);

    }



    // 记录学生考勤

    public void recordAttendance(String studentId, String date, boolean isPresent) {

        Student student = studentManager.getStudentById(studentId);

        if (student != null) {

            AttendanceRecord record = new AttendanceRecord(date, isPresent);

            student.addAttendanceRecord(record);

            System.out.println("学生 " + studentId + " 在 " + date + " 的考勤记录: " + (isPresent ? "出勤" : "缺勤"));

        } else {

            System.out.println("未找到学生: " + studentId);

        }

    }



    // 记录学生答题情况

    public void recordAnswer(String studentId, String subject, boolean isCorrect) {

        Student student = studentManager.getStudentById(studentId);

        if (student != null) {

            ScoreRecord record = new ScoreRecord(subject, isCorrect ? 1 : 0);

            student.addScoreRecord(record);

            System.out.println("学生 " + studentId + " 在 " + subject + " 的答题记录: " + (isCorrect ? "正确" : "错误"));

        } else {

            System.out.println("未找到学生: " + studentId);

        }

    }



    // 导出某人的成绩

    public void exportGrades(String studentID) {
        System.out.println("----------------------------");
        for (Student student : studentManager.getStudentList()) {
            if(student.getId().equals(studentID)){
                System.out.println("学生 ID: " + student.getId() + ", 姓名: " + student.getName());

                System.out.println("考勤记录:");

                for (AttendanceRecord record : student.getAttendanceRecords()) {

                    System.out.println("日期: " + record.getDate() + ", 出勤: " + (record.isPresent() ? "是" : "否"));

                }

                System.out.println("成绩记录:");

                for (ScoreRecord record : student.getScoreRecords()) {

                    System.out.println("科目: " + record.getSubject() + ", 成绩: " + record.getScore());

                }
            }


        }

    }



    // 随机抽取班级、小组和学生，并记录考勤和成绩

    public void randomSelectClassGroupStudent() {

        Class randomClass = getRandomClass();

        if (randomClass != null) {

            System.out.println("随机抽取的班级: " + randomClass.getName());



            Group randomGroup = getRandomGroup(randomClass.getId());

            if (randomGroup != null) {

                System.out.println("随机抽取的小组: " + randomGroup.getName());



                Student randomStudent = getRandomStudentInGroup(randomGroup.getId());

                if (randomStudent != null) {

                    System.out.println("随机抽取的学生: " + randomStudent.getName());



                    // 记录学生考勤

                    System.out.print("请输入考勤日期: ");

                    String date = scanner.next();

                    System.out.print("请输入考勤情况 (1: 出勤, 0: 缺勤): ");

                    boolean isPresent = scanner.nextInt() == 1;

                    recordAttendance(randomStudent.getId(), date, isPresent);



                    // 记录学生答题情况

                    System.out.print("请输入科目: ");

                    String subject = scanner.next();

                    System.out.print("请输入答题情况 (1: 正确, 0: 错误): ");

                    boolean isCorrect = scanner.nextInt() == 1;

                    recordAnswer(randomStudent.getId(), subject, isCorrect);



                    // 根据答题情况给小组评分

                    int groupScore = isCorrect ? 1 : 0; // 假设答对得1分，答错得0分

                    rateGroup(randomGroup.getId(), groupScore);
                    exportGrades(randomStudent.getId());



                } else {

                    System.out.println("该小组没有学生");

                }

            } else {

                System.out.println("该班级没有小组");

            }

        } else {

            System.out.println("没有班级");

        }

    }



}
